﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web;
using System.Text;
using System.Diagnostics;
using Newtonsoft.Json;

namespace Able.Api.Tests
{
    [TestClass]
    public class TestApi
    {
        [TestMethod]
        public void TestMethod1()
        {
            string appkey = "*******************"; //配置您申请的appkey


            //1.菜谱大全
            string url1 = "http://apis.juhe.cn/cook/query.php";

            var parameters1 = new Dictionary<string, string>();

            parameters1.Add("menu", ""); //需要查询的菜谱名
            parameters1.Add("key", appkey);//你申请的key
            parameters1.Add("dtype", ""); //返回数据的格式,xml或json，默认json
            parameters1.Add("pn", ""); //数据返回起始下标
            parameters1.Add("rn", ""); //数据返回条数，最大30
            parameters1.Add("albums", ""); //albums字段类型，1字符串，默认数组

            string result1 = ApiHelper.SendPost(url1, parameters1);

            var newObj1 = JsonConvert.DeserializeObject<Result1>(result1);
            String errorCode1 = newObj1.ErrorCode;

            if (errorCode1 == "0")
            {
                Debug.WriteLine("成功");
                Debug.WriteLine(newObj1);
            }
            else
            {
                //Debug.WriteLine("失败");
                Debug.WriteLine(newObj1.ErrorCode + ":" + newObj1.Reason);
            }


            //2.分类标签列表
            string url2 = "http://apis.juhe.cn/cook/category";

            var parameters2 = new Dictionary<string, string>();

            parameters2.Add("parentid", ""); //分类ID，默认全部
            parameters2.Add("key", appkey);//你申请的key
            parameters2.Add("dtype", ""); //返回数据的格式,xml或json，默认json

            string result2 = ApiHelper.SendPost(url2, parameters2);

            var newObj2 = JsonConvert.DeserializeObject<Result1>(result2);
            String errorCode2 = newObj2.ErrorCode;

            if (errorCode2 == "0")
            {
                Debug.WriteLine("成功");
                Debug.WriteLine(newObj2);
            }
            else
            {
                //Debug.WriteLine("失败");
                Debug.WriteLine(newObj2.ErrorCode + ":" + newObj2.Reason);
            }


            //3.按标签检索菜谱
            string url3 = "http://apis.juhe.cn/cook/index";

            var parameters3 = new Dictionary<string, string>();

            parameters3.Add("cid", ""); //标签ID
            parameters3.Add("key", appkey);//你申请的key
            parameters3.Add("dtype", ""); //返回数据的格式,xml或json，默认json
            parameters3.Add("pn", ""); //数据返回起始下标，默认0
            parameters3.Add("rn", ""); //数据返回条数，最大30，默认10
            parameters3.Add("format", ""); //steps字段屏蔽，默认显示，format=1时屏蔽

            string result3 = ApiHelper.SendPost(url3, parameters3);

            var newObj3 = JsonConvert.DeserializeObject<Result1>(result3);
            String errorCode3 = newObj3.ErrorCode;

            if (errorCode3 == "0")
            {
                Debug.WriteLine("成功");
                Debug.WriteLine(newObj3);
            }
            else
            {
                //Debug.WriteLine("失败");
                Debug.WriteLine(newObj3.ErrorCode + ":" + newObj3.Reason);
            }


            //4.按菜谱ID查看详细
            string url4 = "http://apis.juhe.cn/cook/queryid";

            var parameters4 = new Dictionary<string, string>();

            parameters4.Add("id", ""); //菜谱的ID
            parameters4.Add("key", appkey);//你申请的key
            parameters4.Add("dtype", ""); //返回数据的格式,xml或json，默认json

            string result4 = ApiHelper.SendPost(url4, parameters4);

            var newObj4 = JsonConvert.DeserializeObject<Result1>(result4);
            String errorCode4 = newObj4.ErrorCode;

            if (errorCode4 == "0")
            {
                Debug.WriteLine("成功");
                Debug.WriteLine(newObj4);
            }
            else
            {
                //Debug.WriteLine("失败");
                Debug.WriteLine(newObj4.ErrorCode + ":" + newObj4.Reason);
            }


        }
    }

}
